/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thaihung.common;
import AcessDB.Acess;
import java.sql.*;
/**
 *
 * @author bj
 */
public class SQLQuery {
    
    Statement statement = null;
    Acess ac = null;
    Connection conn = null;
    ResultSet rs = null;

    public SQLQuery()
    {
        ac = new Acess();
        conn = ac.getConnection();
        
    }
    public ResultSet ExcuteQR(String query) throws SQLException
    {     
        try
        { 
            statement = conn.createStatement();
            rs = statement.executeQuery(query);
        }
        catch(SQLException ex)
        {
            throw ex;
        } 
        return rs;
    }
    
    public int Count(String query) throws SQLException
    {
        int count = 0;
        try
        { 
            statement = conn.createStatement();
            rs = statement.executeQuery(query);
            while(rs.next())
                count++;           
        }
        catch(SQLException ex)
        {
            throw ex;
        }     
        return count;
    }
    public int ThoiGian(String query)throws SQLException
    {
        int tg = -1;
        try
        { 
            statement = conn.createStatement();
            rs = statement.executeQuery(query);
            if(rs.next())
                tg = rs.getInt(1);
        }
        catch(SQLException ex)
        {
            throw ex;
        }  
        return tg;
    }
    public void Excute(String query) throws SQLException
    {     
        try
        { 
            statement = conn.createStatement();
            statement.executeUpdate(query);
        }
        catch(SQLException ex)
        {
            throw ex;
        }
    }
    
    public int LayIdentity(String query)throws SQLException
    {
        int identity = -1;
        try
        { 
            statement = conn.createStatement();
            rs = statement.executeQuery(query);
            if(rs.next())
                identity = rs.getInt(1);
        }
        catch(SQLException ex)
        {
            throw ex;
        }  
        return identity;
    }
}
